package cn.com.customer.manager.wigeit.floatview;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


import com.xuexiang.xfloatview.XFloatView;

import com.xuexiang.xutil.app.AppUtils;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.display.DensityUtils;
import com.xuexiang.xutil.display.ScreenUtils;

import java.util.List;

import cn.com.customer.manager.R;
import cn.com.customer.manager.bean.CustomerInfo;

/**
 * 应用切换悬浮窗
 *
 * @author xuexiang
 * @since 2019/1/21 上午11:53
 */
public class AppSwitchView extends XFloatView {
    /**
     * 应用名
     */
    private TextView mTvAppName;
    /**
     * 包名
     */
    private TextView mTvPackageName;

    private Handler mMainHandler = new Handler(Looper.getMainLooper());

    private String mPackageName;
    /**
     * 构造器
     *
     * @param context
     */
    public AppSwitchView(Context context) {
        super(context);
    }

    /**
     * @return 获取根布局的ID
     */
    @Override
    protected int getLayoutId() {
        return R.layout.layout_float_view;
    }

    /**
     * @return 能否移动或者触摸响应
     */
    @Override
    protected boolean canMoveOrTouch() {
        return true;
    }

    /**
     * 初始化悬浮控件
     */
    @Override
    protected void initFloatView() {
        mTvAppName = findViewById(R.id.tv_app_name);
        mTvPackageName = findViewById(R.id.tv_package_name);
        initFloatViewPosition(ScreenUtils.getScreenWidth() - DensityUtils.dip2px(55), (ScreenUtils.getScreenHeight() - getFloatRootView().getMeasuredHeight()) / 2 - getStatusBarHeight());
    }

    /**
     * 初始化监听
     */
    @Override
    protected void initListener() {
        setOnFloatViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    /**
     * 判断 App 是否处于前台
     *
     * @return {@code true}: 是<br>{@code false}: 否
     */
    public static boolean isAppForeground(String packageName) {
        ActivityManager manager = AppUtils.getActivityManager();
        List<ActivityManager.RunningAppProcessInfo> info = manager.getRunningAppProcesses();
        if (info == null || info.size() == 0) {
            return false;
        }
        for (ActivityManager.RunningAppProcessInfo aInfo : info) {
            if (aInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return aInfo.processName.equals(packageName);
            }
        }
        return false;
    }


    public void updateAppInfo(CustomerInfo info) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            mTvAppName.setText(String.format("名字：%s", info.getName()));
            mTvPackageName.setText(String.format("公司：%s", info.getCompanyName()));
        } else {
            mMainHandler.post(new Runnable() {
                @Override
                public void run() {
                    mTvAppName.setText(String.format("名字：%s", info.getName()));
                    mTvPackageName.setText(String.format("公司：%s", info.getCompanyName()));
                }
            });
        }
        Log.e("phone","弹窗信息已更新");
    }

    /**
     * @return 设置悬浮框是否吸附在屏幕边缘
     */
    @Override
    protected boolean isAdsorbView() {
        return true;
    }

    @Override
    public void clear() {
        super.clear();
        mMainHandler.removeCallbacksAndMessages(null);
    }
}
